package tableprocessor;

import java.util.Iterator;
import java.util.List;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 *
 * @author Gleb
 */
public class DBCathegoryDAO
{
    private final static Logger logger = LoggerFactory.getLogger(DBCathegoryDAO.class);
    public static Iterator<DBCathegory> getDBCathegoryList()
    {
        Transaction tx = null;
        List <DBCathegory> rlist = null;
        Session session = SessionFactoryUtil.getInstance().getCurrentSession();
        try
        {
            session.setFlushMode(FlushMode.NEVER);
            tx = session.beginTransaction();
            Query q = session.createQuery("FROM DBCathegory");//"SELECT rlist FROM DBCathegory AS rlist ");
            rlist = q.list();
            tx.commit();
            return rlist.iterator();
        }
        catch (RuntimeException e)
        {
            if (tx != null && tx.isActive())
            {
                try {tx.rollback();}
                catch (HibernateException e1){}
            }
        }
        return null;
    }
    public static void addCathegory(String name, int parent)
    {
      Transaction tx = null;
        Session session = SessionFactoryUtil.getInstance().getCurrentSession();
        try
        {
           // session.setFlushMode(FlushMode.NEVER);
            tx = session.beginTransaction();
            session.save(new DBCathegory(name,parent));
            tx.commit();
        }
        catch (RuntimeException e)
        {
            if (tx != null && tx.isActive())
            {
                try {tx.rollback();}
                catch (HibernateException e1){}
            }
        }

    }
    public static void removeCathegory(int id)
    {
        Transaction tx = null;
        Session session = SessionFactoryUtil.getInstance().getCurrentSession();
        try
        {

           // session.setFlushMode(FlushMode.NEVER);
            tx = session.beginTransaction();
            Query q = session.createQuery("DELETE FROM DBCathegory where id=:idt");
            q.setParameter("idt", id);
            q.executeUpdate();
            //DBColumnDAO
            //q.
           // session.save(new DBCathegory(name,parent));
            tx.commit();
        }
        catch (RuntimeException e)
        {
            e.printStackTrace();
            if (tx != null && tx.isActive())
            {
                try {tx.rollback();}
                catch (HibernateException e1){e1.printStackTrace();}
            }
        }
    }
    public static void renameCathegory(String name , int id, int parent)
    {
        Transaction tx = null;
        Session session = SessionFactoryUtil.getInstance().getCurrentSession();
        try
        {

           // session.setFlushMode(FlushMode.NEVER);
            tx = session.beginTransaction();
            DBCathegory cat = new DBCathegory();
            cat.setId(id);
            cat.setName(name);
            cat.setParent(parent);
            session.update(cat);
            tx.commit();
        }
        catch (RuntimeException e)
        {
            e.printStackTrace();
            if (tx != null && tx.isActive())
            {
                try {tx.rollback();}
                catch (HibernateException e1){e1.printStackTrace();}
            }
        }
    }
}
